function [data2] = loadDataFilesCtsTime(dataId)

if dataId == 3
	timeAggLevel = 'day';
	geoAggLevel =  'CantonId';
	
	% Dataset for covariates
	datasetName = 'CantonId_day';
	datasetFile = sprintf('../../data/data_mat/covariates/covariates_%s.mat', datasetName);

	% Dataset for transactions (consumer adoptions) in continuous time
	inputConsumerAdoptionsFile = '../../data/data_mat/platformData/consumerAdoptionsData_CantonId_ctsTime.mat';
	
	% Dataset for provider entries and provider exits in continuous time
	inputProviderMovementsFile = '../../data/data_mat/platformData/providerMovementsData_CantonId_ctsTime.mat';
end

if strcmp(timeAggLevel, 'week'); tt_subset_idxes = 1:68;  end;
if strcmp(timeAggLevel, 'day');  tt_subset_idxes = 1:476; end;

locationAggregationsFolder = sprintf('../../data/data_mat/timeLocationAggregations/location/%s', geoAggLevel);
timeAggregationsFolder     = sprintf('../../data/data_mat/timeLocationAggregations/time/%s',     timeAggLevel);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%% LOAD TRANSACTIONS DATA (consumer adoptions) %%%%%%%%%%
disp(sprintf('Loading %s...', inputConsumerAdoptionsFile));
load(inputConsumerAdoptionsFile, 'timestampsData');
consumerAdoptionData = timestampsData; clear timestampsData;
	% consumerAdoptionData:	object
	% 	.geoLabels: 			cell(K,1) of strings
	% 	.timestamps:			NumAdoptions x 1
	% 	.kk_consumer:				NumAdoptions x 1 (integer)
	% 	.kk_provider:				NumAdoptions x 1 (integer)

geoLabels = consumerAdoptionData.geoLabels;
K = length(geoLabels);

% Streamline/recode consumerAdoptionData
consumerAdoptionData = rmfield(consumerAdoptionData, 'geoLabels');
consumerAdoptionData.kk_consumer = double(consumerAdoptionData.kk_consumer);
consumerAdoptionData.kk_provider = double(consumerAdoptionData.kk_provider);

%%%%%%%%%% LOAD PROVIDER CREATIONS AND DELETIONS %%%%%%%%%%
disp(sprintf('Loading %s...', inputProviderMovementsFile));
load(inputProviderMovementsFile, 'timestampsData');
platformEventData = timestampsData; clear timestampsData;
	% platformEventData:	object
	% 	.geoLabels: 			cell(K,1) of strings
	% 	.timestamps:			NumEvents x 1
	% 	.kk_idxes:				NumEvents x 1 (integer)
	% 	.jj_idxes:				NumEvents x 1 (integer) --> code indicating event type
	%	.jj_labels:				cell that translates jj_idxes to meaning

% Check consistency of geoLabels
if ~isequal(platformEventData.geoLabels, geoLabels); error('Inconsistent geoLabels'); end;

% Isolate provider creations
providerCreation_idxes = find(platformEventData.jj_idxes == find(strcmp(platformEventData.jj_labels, 'NewProviders')));
providerCreationData.timestamps = platformEventData.timestamps(providerCreation_idxes);
providerCreationData.kk_provider   = double(platformEventData.kk_idxes(providerCreation_idxes));

% Isolate provider deletions
providerDeletion_idxes = find(platformEventData.jj_idxes == find(strcmp(platformEventData.jj_labels, 'DeletedProviders')));
providerDeletionData.timestamps = platformEventData.timestamps(providerDeletion_idxes);
providerDeletionData.kk_provider   = double(platformEventData.kk_idxes(providerDeletion_idxes));

clear platformEventData providerCreation_idxes providerDeletion_idxes;


%%%%%%%%%% GET INITIAL CONDITIONS (before tt = 0) %%%%%%%%%%
% Make initLaggedConsumersOrig and initLaggedConsumersDest
idxes = find(consumerAdoptionData.timestamps < 0);
initLaggedConsumersOrig = accumarray(consumerAdoptionData.kk_consumer(idxes), consumerAdoptionData.kk_consumer(idxes), [K 1], @length); % K_consumer x 1
initLaggedConsumersDest = accumarray(consumerAdoptionData.kk_provider(idxes),  consumerAdoptionData.kk_provider(idxes),  [K 1], @length); % K_provider x 1

% Make initLaggedProviders
idxes = find(providerCreationData.timestamps < 0);
prevProviderCreations = accumarray(providerCreationData.kk_provider(idxes), providerCreationData.kk_provider(idxes), [K 1], @length); % K_provider x 1
idxes = find(providerDeletionData.timestamps < 0);
prevProviderDeletions = accumarray(providerDeletionData.kk_provider(idxes), providerDeletionData.kk_provider(idxes), [K 1], @length); % K_provider x 1
initLaggedProviders = prevProviderCreations - prevProviderDeletions; % K_provider x 1
clear prevProviderCreations prevProviderDeletions idxes;



%%%%%%%%%% COMBINE ORDERED EVENTS TOGETHER %%%%%%%%%%
% Make a matrix that contains all events (consumer adoption, provider adoption, provider deletion, day change)
% Here I assume periodicity at day level.
periodChanges = tt_subset_idxes';

NumConsumerAdoptions = length(consumerAdoptionData.timestamps);
NumProviderCreations  = length(providerCreationData.timestamps);
NumProviderDeletions  = length(providerDeletionData.timestamps);
NumPeriodChanges   = length(tt_subset_idxes);

consumerAdoptionData = [consumerAdoptionData.timestamps consumerAdoptionData.kk_consumer consumerAdoptionData.kk_provider 1*ones(NumConsumerAdoptions, 1)]; % NumConsumerCreations x 4
providerCreationData  = [providerCreationData.timestamps -1*ones(NumProviderCreations, 1) providerCreationData.kk_provider  2*ones(NumProviderCreations, 1)];  % NumProviderCreations x 4
providerDeletionData  = [providerDeletionData.timestamps -1*ones(NumProviderDeletions,1)  providerDeletionData.kk_provider  3*ones(NumProviderDeletions,1)];   % NumProviderDeletions x 4
periodChangeData   = [periodChanges                -1*ones(NumPeriodChanges,1)   -1*ones(NumPeriodChanges,1) 4*ones(NumPeriodChanges,1)];    % NumPeriods x 1

allEventsData = [consumerAdoptionData; providerCreationData; providerDeletionData; periodChangeData]; % NumEvents x 4

% Clean up
clear consumerAdoptionData providerCreationData providerDeletionData periodChangeData;
clear NumConsumerAdoptions NumProviderCreations NumProviderDeletions NumPeriodChanges;


%%%%%%%%%% CONSTRUCT SPELLS FROM ORDERED EVENTS %%%%%%%%%%
% Remove events before tt=0
event_dates = allEventsData(:,1);
allEventsData = allEventsData(event_dates >= 0,:); % I am assuming periodicity at day level here!
clear event_dates;

% Remove events after last day considered
event_dates = allEventsData(:,1);
allEventsData = allEventsData(event_dates <= max(tt_subset_idxes),:); % I am assuming periodicity at day level here!
clear event_dates;

% Sort allEventsData by increasing timestamp
[~,sortidxes] = sort(allEventsData(:,1));
allEventsData = allEventsData(sortidxes,:);
clear sortidxes;

% Compute Y
NumSpells = size(allEventsData,1);
Y.consumerAdoptions = sparse(NumSpells, K*K); % NumSpells x (K_consumer * K_provider)  <=> NumSpells x (K1*K2)
Y.consumerAdoptionsOrig = sparse(NumSpells, K); % NumSpells x K_consumer  <=> NumSpells x K1
Y.consumerAdoptionsDest = sparse(NumSpells, K); % NumSpells x K_provider  <=> NumSpells x K2
Y.providerCreations = sparse(NumSpells, K);
Y.providerDeletions = sparse(NumSpells, K);

for tt = 1:NumSpells
	if allEventsData(tt,4) == 1 % consumer adoption
		kk_consumer = allEventsData(tt,2);
		kk_provider  = allEventsData(tt,3);
		myidx = sub2ind([K,K],kk_consumer,kk_provider);
		Y.consumerAdoptions(tt, myidx) = 1;
		Y.consumerAdoptionsOrig(tt, kk_consumer) = 1;
		Y.consumerAdoptionsDest(tt, kk_provider)  = 1;
	end
	if allEventsData(tt,4) == 2 % provider creation
		kk_provider = allEventsData(tt,3);
		Y.providerCreations(tt,kk_provider) = 1;
	end
	if allEventsData(tt,4) == 3 % provider deletion
		kk_provider = allEventsData(tt,3);
		Y.providerDeletions(tt,kk_provider) = 1;
	end
end
clear kk_consumer kk_provider myidx;

% Make laggedProviders
providerDeltas = Y.providerCreations-Y.providerDeletions;
laggedProviders = initLaggedProviders' + cumsum(providerDeltas, 1) - providerDeltas; % NumSpells x K_providers
clear providerDeltas;

% Remove those spells for which the consumer adopts in location k' where there is no provider
good_spells = ones(NumSpells,1);
for tt = 1:NumSpells
	if allEventsData(tt,4) == 1
		kk_provider = allEventsData(tt,3);
		if laggedProviders(tt,kk_provider) == 0
			good_spells(tt) = 0;
		end
	end
end
clear kk_provider;

good_spells = find(good_spells==1);
allEventsData         = allEventsData(good_spells,:);
Y.consumerAdoptions     = Y.consumerAdoptions(good_spells,:);
Y.consumerAdoptionsOrig = Y.consumerAdoptionsOrig(good_spells,:);
Y.consumerAdoptionsDest = Y.consumerAdoptionsDest(good_spells,:);
Y.providerCreations      = Y.providerCreations(good_spells,:);
Y.providerDeletions      = Y.providerDeletions(good_spells,:);
laggedProviders          = laggedProviders(good_spells,:);
NumSpells = size(allEventsData,1);

clear good_spells;

% Make laggedConsumersOrig, laggedConsumersDest
laggedConsumersOrig = initLaggedConsumersOrig' + cumsum(Y.consumerAdoptionsOrig, 1) - Y.consumerAdoptionsOrig; % NumSpells x K_consumers
laggedConsumersDest = initLaggedConsumersDest' + cumsum(Y.consumerAdoptionsDest, 1) - Y.consumerAdoptionsDest; % NumSpells x K_providers

% Compute spell durations
event_dates = allEventsData(:,1);         % NumSpells x 1
spellStarts = [0; event_dates(1:end-1)];  % NumSpells x 1
spellEnds   = event_dates;                % NumSpells x 1
spellDurations = spellEnds - spellStarts; % NumSpells x 1
clear event_dates;

% Make mapping from spell to day (at start of spell) --> I am assuming periodicity at day level here!
spell2period = make_integers(floor(spellStarts) +  1); % NumSpells x 1 (gives values between 1 and NumPeriods)


%%%% Store everything into an object ctsTimeData
ctsTimeData.NewConsumers    = Y.consumerAdoptions; % NumSpells x (K1*K2) - sparse
ctsTimeData.NewProviders     = Y.providerCreations;  % NumSpells x K       - sparse
ctsTimeData.DeletedProviders = Y.providerDeletions;  % NumSpells x K       - sparse

ctsTimeData.laggedConsumersOrig = laggedConsumersOrig; % NumSpells x K1  - sparse
ctsTimeData.laggedConsumersDest = laggedConsumersDest; % NumSpells x K2  - sparse
ctsTimeData.laggedProviders      = laggedProviders;      % NumSpells x K   - sparse

ctsTimeData.spellDurations    = spellDurations;    % NumSpells x 1
ctsTimeData.spell2period      = spell2period;      % NumSpells x 1 (integer between 1 and NumPeriods)


%%%%%%%%%% LOAD PERIODIC DATA of covariates %%%%%%%%%
configPeriodicData.datasetFile                = datasetFile;
configPeriodicData.timeAggLevel               = timeAggLevel;
configPeriodicData.geoAggLevel                = geoAggLevel;
configPeriodicData.surfaceVar_Zname           = 'Area';
configPeriodicData.laggedAdoptionEffects      = {{}}; % (ignored any way...)
configPeriodicData.locationAggregationsFolder = locationAggregationsFolder;
configPeriodicData.timeAggregationsFolder     = timeAggregationsFolder;


data_tmp = loadPeriodicData(configPeriodicData, tt_subset_idxes);
periodicData      = data_tmp.periodicData;
periodLabels      = data_tmp.periodLabels;
population        = data_tmp.population;
surfaces          = data_tmp.surfaces;
incomingCommuters = data_tmp.incomingCommuters;
touristBeds       = data_tmp.touristBeds;
surfaceName       = data_tmp.surfaceName;

assert(isequal(data_tmp.geoLabels, geoLabels));

clear data_tmp;


%%%%%% Store everything into data2
data2.ctsTimeData  = ctsTimeData;
data2.periodicData = periodicData;

data2.geoLabels    = geoLabels;
data2.periodLabels  = periodLabels;

data2.population   = population;
data2.surfaces     = surfaces;
data2.incomingCommuters = incomingCommuters;
data2.touristBeds       = touristBeds;
data2.surfaceName       = surfaceName;

end
